<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form
      label-position="left"
      label-width="80px"
      :model="pwdForm"
      :rules="pwdRules"
      ref="formRef"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input type="password" v-model="pwdForm.old_pwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input type="password" v-model="pwdForm.new_pwd"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="re_pwd">
        <el-input type="password" v-model="pwdForm.re_pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doChange">提交修改</el-button>
        <el-button @click="$refs.formRef.resetFields()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "UserPwd",
  data() {
    return {
      //表单数据
      pwdForm: {
        old_pwd: "",
        new_pwd: "",
        re_pwd: ""
      },
      //表单校验
      pwdRules: {
        old_pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码长度必须是6-15位的非空字符串",
            trigger: "blur"
          }
        ],
        new_pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码长度必须是6-15位的非空字符串",
            trigger: "blur"
          }
        ],
        re_pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "密码长度必须是6-15位的非空字符串",
            trigger: "blur"
          },
          {
            validator: (rule, value, callback) => {
              if (value !== this.pwdForm.new_pwd) {
                return callback(new Error("两次新密码不一致！"));
              }
              callback();
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    doChange() {
      //(1)表单校验
      this.$refs.formRef.validate(async valide=>{
        //(2)成功发送ajax
        if(valide){
          const {data:res} = await this.$axios.patch('/my/updatepwd',this.pwdForm)
          //提示用户结果
          this.$message.success( res.message )
        }
      })
    }
  }
};
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
